import pandas as pd

# 读取 Excel 文件
file_path_1 = r'E:\ch\ch-1.xlsx'
df1 = pd.read_excel(file_path_1, header=0)

file_path_2 = r'E:\ch\ch-8.xlsx'
df2 = pd.read_excel(file_path_2, header=0)

print(222)


def deal(df: pd.DataFrame, source: str):
    # 添加数据来源字段
    df["数据来源"] = source

    # 定义需要保留的列名（按指定顺序）
    itemNames = ["实施编码", "实施清单名称", "事项名称短名", "日常用语", "数据来源"]

    # 存储最终输出内容
    output_lines = []

    # 构建每行内容，严格按 itemNames 顺序取值
    for _, row in df.iterrows():
        line_parts = []
        for col in itemNames:
            value = row[col]
            value_str = '' if pd.isna(value) else str(value)
            line_parts.append(value_str)
        output_lines.append(';'.join(line_parts))

    # 动态生成文件名
    filename = f"output_name{source}.md"

    # 写入文件并处理异常
    try:
        with open(filename, 'w', encoding='utf-8') as f:
            f.write('\n'.join(output_lines))
    except Exception as e:
        print(f"写入文件 {filename} 时发生错误: {e}")


# 调用函数
deal(df1, "1")
deal(df2, "2")
